﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Dynamic;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for WindowsPostMessage.xaml
	/// </summary>
	public partial class WindowsPostMessage : Window
	{
		CFacebookServer fbServer; 
		string id;
		public WindowsPostMessage(double _Y, string _id, string _AccessToken)
		{
			this.InitializeComponent();
			// Insert code required on object creation below this point.
			fbServer = new CFacebookServer(_AccessToken);
			id = _id;
            this.Left = SystemParameters.WorkArea.Width - this.Width - 330;
			this.Top = _Y - this.ActualHeight/2;
            message.Focus();
		}


		private void message_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			// TODO: Add event handler implementation here.
			if (e.Key == Key.Escape)
				this.Close();
			if ((e.KeyboardDevice.Modifiers == ModifierKeys.None) && e.Key == Key.Enter)
            {
                // Do post message to facebook
                e.Handled = true;
				
                PostMessage();
            }
		}
		void PostMessage()
		{
            if (message.Text == null)
            {
                MessageBox.Show("Your message must not be empty");
                return;
			}
			dynamic paramenters = new ExpandoObject();
			paramenters.message = message.Text;
			string result = fbServer.Post(id + "/feed", paramenters);
			if (result != null)
            {
                MessageBox.Show("Message has been posted successfully!");
                // Reload messages
                this.Close();
            }
            else
            {
                MessageBox.Show("Can not post your message!");
            }
			//MessageBox.Show(this, "Do post message", "info");
		}
	}
}